#include <functional> // std::function, std::bind
#include <iostream>

//------------------------------------------------------------------------------

// run functions with return type R and without arguments

template <typename R>
R call(std::function<R()> f) {
    // R() - function type: R - type of returned value, () - empty arguments list
    // http://en.cppreference.com/w/cpp/utility/functional/function

    // invoke operator () on std::function object to run wrapped callable entity (lambda function, function, bind wrapper)
    // http://en.cppreference.com/w/cpp/utility/functional/function/operator%28%29
    return f();
}

//------------------------------------------------------------------------------

int get_random_number() {
    return 4; // chosen by fair dice roll
              // guaranteed to be random
              // http://xkcd.com/221/
}

int pow2(int x) {
    return x * x;
}

int main() {
    // call normal function
    std::cout << "normal function: " << call<int>( get_random_number ) << std::endl;

    // call lambda function
    std::cout << "lambda function: " << call<int>( []() { return 42; } ) << std::endl;

    // call std::bind wrapper
    auto pow2_test = std::bind(pow2, 2);
    std::cout << "std::bind wrapper: " << call<int>( pow2_test ) << std::endl;

    return 0;
}
